<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% 
  content_for :page_title, t("#titles.error_reports", "Error Reports")
  add_crumb t("#crumbs.error_reports", "Error Reports")
%>

<%= form_for @current_user, :url => '/error_reports', :html => { :method => :get }  do |f| %>
  <% if error_search_enabled? %>
    <%= t :message_contains, "Message contains" %>
    <input type="text" name="message" value="<%= @message %>" style="width: 250px;"/>
  <% end %>
  <select name="category">
    <%= options_for_select([[t(:all_categories, " - All Categories -"), nil]] + ErrorReport.categories, params[:category]) %>
  </select>
  <button type="submit" class="btn"><%= t "#buttons.search", "Search" %></button>
<% end %>

<% content_for :pagination do %>
  <% if params[:action] == 'index' %>
    <%= will_paginate(@reports, :page_links => false) %>
  <% end %>
<% end %>

<%= yield :pagination %>

<% @reports.each do |report| %>
  <div style="margin-bottom: 20px;">
    <h3>
      <%= link_to "##{report.id}", error_url(report) %>
      <% if @reports.length > 1 %>
        <%= truncate(report.message, :length => 80) %>
      <% else %>
        <%= report.message %>
      <% end %>
    </h3>

    <div style="border: 1px solid #eee; -moz-border-radius: 5px; padding: 5px 20px;">
      <% if report.account_id %>
        <%= before_label :account, "account" %> <%= link_to report.account.try(:name), account_url(report.account_id) %><br />
      <% end %>
      <%= before_label :category, "category" %> <%= report.category || t(:default_category, 'default') %><br/>
      <%= before_label :created_at, "created at" %> <%= report.created_at %><br />
      <% if report.user_id %>
        <%= before_label :user, "user" %> <%= link_to report.user.try(:name), user_url(report.user_id) %><br />
      <% end %>
      <% if report.url.present? %>
        <%= before_label :url, "url" %>
        <%=
          if report.safe_url?
            link_to report.url, report.url
          else
            report.url
          end
        %><br />
      <% end %>
      <% if report.request_context_id %>
        <%= before_label :request_context_id, "request context id" %> <%= report.request_context_id %><br />
      <% end %>
      <% if report.comments %>
        <%= before_label :comments, "comments" %> <%= report.comments %><br />
      <% end %>
      <% (report.data || {}).to_a.sort.each do |k,v| %>
        <% next if k.to_s == "exception_message" %>
        <br><%= before_label k.to_s %> <%= v %>
      <% end %>
      <br />
      <pre style="font-size: 0.8em; margin-left: 20px; <%= "max-height: 150px;" if @reports.length > 1 %> overflow: auto;">
        <%= report.data.try(:[], "exception_message") %><br />
        <%= report.backtrace %>
      </pre>
    </div>
  </div>
<% end %>

<%= yield :pagination %>
